var type = "";
var name = "";
var active = {
    initColumn: function () {
        // 获取栏目列表
        $.ajax({
            url: "/interface/getColumnList",
            data: {},
            dataType: "json",
            type: "post",
            async: false,
            success: function (res) {
                if (res.returnCode === "00") {
                    $(".ysh-list-left ul").empty();
                    var columnList = res.list;
                    if (!!columnList && columnList.length > 0) {
                        for (var j = 0; j < columnList.length; j++) {
                            var str = '<a class="column' + columnList[j].ID + '" href="list.html?type=' + columnList[j].ID + '">\n' +
                                '          <li>' + columnList[j].COLUMN_NAME + '</li>\n' +
                                '      </a>';
                            $(".ysh-list-left ul").append(str);
                        }
                        var id = QueryUtils.GetQueryString("id");
                        active.initData(id);
                    }

                } else if (res.returnCode === "01") {
                    console.log(res);
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                console.error(XMLHttpRequest.status);
                console.error(XMLHttpRequest.readyState);
                console.error(textStatus);
            }
        });
    },
    initData: function (id) {
        $.ajax({
            url: "/interface/getInformationById",
            data: {ID: id},
            dataType: "json",
            type: "post",
            async: false,
            success: function (res) {
                if (res.returnCode === "00") {
                    var data = res.data;
                    if (!!data && !!data.COLUMN_NAME) {
                        $(".type-name").html(data.COLUMN_NAME);
                        $(".column" + data.COLUMN_ID + " li").addClass("active");

                        $(".ysh-detail-page-title").html(data.TITLE);
                        $(".source-time span").eq(0).html("来源 : " + data.SOURCE);
                        $(".source-time span").eq(1).html(active.toDateString(new Date(data.GMT_CREATE)));
                        $(".ysh-detail-page-main").html(data.CONTENT);
                    } else {
                        $(".type-name").html("市场信息");
                        $(".ysh-detail-page-title").html("资讯不存在");
                        $(".source-time").remove();
                        $(".ysh-detail-page-main").remove();
                    }
                } else if (res.returnCode === "01") {
                    console.log(res);
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                console.error(XMLHttpRequest.status);
                console.error(XMLHttpRequest.readyState);
                console.error(textStatus);
            }
        });
    },
    //时间戳的处理
    toDateString: function (d, format) {
        var date = new Date(d || new Date())
            , ymd = [
            active.digit(date.getFullYear(), 4)
            , active.digit(date.getMonth() + 1)
            , active.digit(date.getDate())
        ]
            , hms = [
            active.digit(date.getHours())
            , active.digit(date.getMinutes())
            , active.digit(date.getSeconds())
        ];

        format = format || 'yyyy-MM-dd HH:mm:ss';

        return format.replace(/yyyy/g, ymd[0])
            .replace(/MM/g, ymd[1])
            .replace(/dd/g, ymd[2])
            .replace(/HH/g, hms[0])
            .replace(/mm/g, hms[1])
            .replace(/ss/g, hms[2]);
    },
    //数字前置补零
    digit: function (num, length, end) {
        var str = '';
        num = String(num);
        length = length || 2;
        for (var i = num.length; i < length; i++) {
            str += '0';
        }
        return num < Math.pow(10, length) ? str + (num | 0) : num;
    }
};

$(function () {
    active.initColumn();
});
